use crate::context::Context;
use crate::interface::{AsyncHandle, EventHandle, EventTrigger};
use std::marker::PhantomData;

pub struct Handle<T, F> {
    inner: F,
    i: PhantomData<T>,
}

impl<T, F> Handle<T, F>
where
    T: EventTrigger,
    F: AsyncHandle<T> + Sync + Send,
{
    pub fn new(inner: F) -> Handle<T, F> {
        Self {
            inner,
            i: PhantomData::default(),
        }
    }
}
#[async_trait::async_trait]
impl<T, F> EventHandle for Handle<T, F>
where
    T: EventTrigger,
    F: AsyncHandle<T> + Sync + Send,
{
    fn event_type_id(&self) -> u64 {
        wd_tools::ptr::type_id::<T>()
    }

    async fn launch(
        &self,
        ctx: Context,
        req: Box<dyn EventTrigger>,
    ) -> anyhow::Result<Box<dyn EventTrigger>> {
        let t = wd_tools::ptr::force_box_to_var::<_, T>(req);
        let t = self.inner.call(ctx, t).await?;
        Ok(Box::new(t))
    }
}
